/*
 * @Author: 谢陈洁
 * @Date: 2025-06-18 14:44:56
 * @LastEditors: 谢陈洁
 * @LastEditTime: 2025-06-23 14:50:43
 * @Description: file content
 */
import { getAllDictionary, getDictionaryItemList } from '@/services/apis/system/dictionary'
import { defineStore } from "pinia";
export interface DictionaryState {
  dictionary: Record<string, any>
}
export const useDictStore = defineStore('dict', {
  state: (): DictionaryState => {
    return {
      dictionary: {}
    }
  },
  actions: {
    async loadDictByCode(code: string) {
      const res = await getDictionaryItemList({code})
      this.dictionary[code] = res.list
      return res.list
    },

    async getDict(code: string) {
      console.log('getDict', code, this.dictionary)
      const dicItems = this.dictionary[code]
      if (dicItems && dicItems.length) {
        return Promise.resolve(dicItems)
      } else {
        return await this.loadDictByCode(code)
      }
    },
    async loadAllDict() {
      const res = await getAllDictionary({})
      this.dictionary = res.list
    },
    clearDict() {
      this.dictionary = {}
    }
  },
  persist: true
})
